package com.netease.nim.demo.common.ui.imageview;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class DrawableCompat {
	public static final Bitmap getBitmapImmutableCopy(Resources res, int id) {
		return getBitmap(res.getDrawable(id)).copy(Config.RGB_565, false);
	}
	
	public static final Bitmap getBitmap(Resources res, int id) {
		return getBitmap(res.getDrawable(id));
	}
	
	public static final Bitmap getBitmap(Drawable dr) {
		if (dr == null) {
			return null;
		}
		
		if (dr instanceof BitmapDrawable) {
			return ((BitmapDrawable) dr).getBitmap();
		}
		
		return null;
	}
	
	public static final boolean isBitmap(Drawable dr) {
		if (dr == null) {
			return false;
		}
		
		if (dr instanceof BitmapDrawable) {
			return true;
		}
		
		return false;
	}
	
	public static final Drawable createBitmap() {
		return new BitmapDrawable();
	}
	
	public static final Drawable createBitmap(Bitmap bitmap) {
		return new BitmapDrawable(bitmap);
	}
	
	public static final Drawable createBitmap(Resources res, Bitmap bitmap) {
		return new BitmapDrawable(res, bitmap);
	}
	
	public static final Drawable newDrawable(Resources res, Drawable dr) {
		if (dr instanceof RecyclingDrawable) {
			dr = ((RecyclingDrawable) dr).newDrawable(res);
		} else {
			dr = dr.getConstantState().newDrawable(res);
		}
	
		if (dr instanceof RecyclingDrawable) {
			((RecyclingDrawable) dr).setIsDisplayed(true);
		}
		
		return dr;
	}
	
	public static final void releaseDrawable(Drawable dr) {
		if (dr instanceof RecyclingDrawable) {
			((RecyclingDrawable) dr).setIsDisplayed(false);
		}
	}
}
